package admingui;



import java.beans.PropertyChangeEvent;
import java.text.NumberFormat;
import java.util.ArrayList;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.text.MaskFormatter;
import server.Server;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdminGUI.java
 *
 * Created on May 12, 2011, 8:36:30 PM
 */

/**
 *
 * @author miguelkhoury
 */
public class adminGUI extends javax.swing.JFrame {


    Server server = new Server();
    String username = "admin";
    String password = "eece450";
    Admin admin = new Admin(username,password,server);
    int gameNo = 12;

    public adminGUI() {
        initComponents();
        setTitle("Administrator login");
        adminmain.setVisible(false);
        adminlogin.setVisible(true);
        setSize(330,230);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        adminlogin = new javax.swing.JPanel();
        usernamelabel = new javax.swing.JLabel();
        passwordlabel = new javax.swing.JLabel();
        usernametext = new javax.swing.JTextField();
        loginbutton = new javax.swing.JButton();
        passwordtext = new javax.swing.JPasswordField();
        adminmain = new javax.swing.JPanel();
        team1label = new javax.swing.JLabel();
        selectgamelabel = new javax.swing.JLabel();
        updateevents = new javax.swing.JButton();
        updatescores = new javax.swing.JButton();
        selectgamecombo = new javax.swing.JComboBox();
        eventstext = new javax.swing.JTextField();
        score1text = new javax.swing.JFormattedTextField();
        score2text = new javax.swing.JTextField();
        scorelabel = new javax.swing.JLabel();
        eventslabel = new javax.swing.JLabel();
        team2label = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Administrator");
        setName("adminframe"); // NOI18N
        setPreferredSize(new java.awt.Dimension(640, 460));
        setSize(new java.awt.Dimension(500, 400));

        adminlogin.setName("adminlogin"); // NOI18N
        adminlogin.setOpaque(false);
        adminlogin.setPreferredSize(new java.awt.Dimension(350, 190));

        usernamelabel.setText("Username");
        usernamelabel.setName("usernamelabel"); // NOI18N

        passwordlabel.setText("Password");

        usernametext.setText("admin");
        usernametext.setName("usernametext"); // NOI18N
        usernametext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                usernametextActionPerformed(evt);
            }
        });

        loginbutton.setText("Login");
        loginbutton.setName("loginbutton"); // NOI18N
        loginbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginbuttonActionPerformed(evt);
            }
        });

        passwordtext.setText("eece450");
        passwordtext.setName("passwordtext"); // NOI18N

        org.jdesktop.layout.GroupLayout adminloginLayout = new org.jdesktop.layout.GroupLayout(adminlogin);
        adminlogin.setLayout(adminloginLayout);
        adminloginLayout.setHorizontalGroup(
            adminloginLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 350, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, adminloginLayout.createSequentialGroup()
                .add(75, 75, 75)
                .add(adminloginLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(usernamelabel)
                    .add(passwordlabel))
                .add(18, 18, 18)
                .add(adminloginLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(loginbutton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
                    .add(adminloginLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(passwordtext, 0, 0, Short.MAX_VALUE)
                        .add(usernametext, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 111, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(81, 81, 81))
        );
        adminloginLayout.setVerticalGroup(
            adminloginLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 190, Short.MAX_VALUE)
            .add(adminloginLayout.createSequentialGroup()
                .add(42, 42, 42)
                .add(adminloginLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(usernamelabel)
                    .add(usernametext, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(adminloginLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(passwordlabel)
                    .add(passwordtext, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(loginbutton)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        selectgamelabel.setText("Select game");

        updateevents.setText("Update events");
        updateevents.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateeventsActionPerformed(evt);
            }
        });

        updatescores.setText("Update score");
        updatescores.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updatescoresActionPerformed(evt);
            }
        });

        selectgamecombo.setModel(new javax.swing.DefaultComboBoxModel());
        selectgamecombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectgamecomboActionPerformed(evt);
            }
        });

        eventstext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eventstextActionPerformed(evt);
            }
        });

        score1text.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                score1textActionPerformed(evt);
            }
        });

        score2text.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                score2textActionPerformed(evt);
            }
        });

        scorelabel.setText("Scores");

        eventslabel.setText("Events");

        org.jdesktop.layout.GroupLayout adminmainLayout = new org.jdesktop.layout.GroupLayout(adminmain);
        adminmain.setLayout(adminmainLayout);
        adminmainLayout.setHorizontalGroup(
            adminmainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(adminmainLayout.createSequentialGroup()
                .add(37, 37, 37)
                .add(adminmainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(selectgamelabel)
                    .add(scorelabel)
                    .add(eventslabel))
                .add(35, 35, 35)
                .add(adminmainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, eventstext, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 300, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, adminmainLayout.createSequentialGroup()
                        .add(14, 14, 14)
                        .add(team1label))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, adminmainLayout.createSequentialGroup()
                        .add(score1text, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 50, Short.MAX_VALUE)
                        .add(updatescores)
                        .add(39, 39, 39)
                        .add(adminmainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(team2label)
                            .add(score2text, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, selectgamecombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 300, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(157, 157, 157))
            .add(adminmainLayout.createSequentialGroup()
                .add(230, 230, 230)
                .add(updateevents)
                .addContainerGap(241, Short.MAX_VALUE))
        );
        adminmainLayout.setVerticalGroup(
            adminmainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(adminmainLayout.createSequentialGroup()
                .add(48, 48, 48)
                .add(adminmainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(selectgamelabel)
                    .add(selectgamecombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(adminmainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(adminmainLayout.createSequentialGroup()
                        .add(team2label)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(adminmainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(score2text, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(updatescores)))
                    .add(adminmainLayout.createSequentialGroup()
                        .add(24, 24, 24)
                        .add(scorelabel))
                    .add(adminmainLayout.createSequentialGroup()
                        .add(team1label)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(score1text, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 23, Short.MAX_VALUE)
                .add(adminmainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(eventslabel)
                    .add(eventstext, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 185, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(updateevents)
                .add(67, 67, 67))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(adminmain, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(0, 147, Short.MAX_VALUE)
                    .add(adminlogin, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(0, 147, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(adminmain, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(0, 137, Short.MAX_VALUE)
                    .add(adminlogin, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(0, 137, Short.MAX_VALUE)))
        );

        adminlogin.getAccessibleContext().setAccessibleName("adminlogin");

        pack();
    }// </editor-fold>//GEN-END:initComponents



    private void updatescoresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updatescoresActionPerformed
        
        admin.updateGameScore(gameNo,Integer.parseInt(score1text.getText()),
                Integer.parseInt(score2text.getText()));

}//GEN-LAST:event_updatescoresActionPerformed

    private void selectgamecomboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectgamecomboActionPerformed
        
        gameNo = selectgamecombo.getSelectedIndex()+1;
        ArrayList A = admin.getGameScores(gameNo);
        score1text.setText(""+A.get(0));
        score2text.setText(""+A.get(1));
        String teams[] = admin.getGameTeams(gameNo);
        team1label.setText(teams[0]);
        team2label.setText(teams[1]);
        String events = admin.getGameEvents(gameNo);
        if (events.equals("null")){eventstext.setText("");}
        else eventstext.setText(events);

}//GEN-LAST:event_selectgamecomboActionPerformed

    private void eventstextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eventstextActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_eventstextActionPerformed

    private void score2textActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_score2textActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_score2textActionPerformed

    private void usernametextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_usernametextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_usernametextActionPerformed

    private void loginbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginbuttonActionPerformed

        if (usernametext.getText().matches(username)
            &&passwordtext.getText().equals(password)){
            adminlogin.setVisible(false);
            adminmain.setVisible(true);
            adminmain.setLocation(100,100);
            setSize(670,450);
            String[] items = admin.fillComboGames();
            selectgamecombo.setModel(new javax.swing.DefaultComboBoxModel(items));
        }
        else {
            JOptionPane.showMessageDialog(null,
                    "Invalid password. Try again.",
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_loginbuttonActionPerformed

    
    private void updateeventsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateeventsActionPerformed
        
        admin.updateGameEvents(gameNo, eventstext.getText());

    }//GEN-LAST:event_updateeventsActionPerformed

    private void score1textActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_score1textActionPerformed
       
    }//GEN-LAST:event_score1textActionPerformed


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new adminGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel adminlogin;
    private javax.swing.JPanel adminmain;
    private javax.swing.JLabel eventslabel;
    private javax.swing.JTextField eventstext;
    private javax.swing.JButton loginbutton;
    private javax.swing.JLabel passwordlabel;
    private javax.swing.JPasswordField passwordtext;
    private javax.swing.JTextField score1text;
    private javax.swing.JTextField score2text;
    private javax.swing.JLabel scorelabel;
    private javax.swing.JComboBox selectgamecombo;
    private javax.swing.JLabel selectgamelabel;
    private javax.swing.JLabel team1label;
    private javax.swing.JLabel team2label;
    private javax.swing.JButton updateevents;
    private javax.swing.JButton updatescores;
    private javax.swing.JLabel usernamelabel;
    private javax.swing.JTextField usernametext;
    // End of variables declaration//GEN-END:variables

}
